package com.bai.edu.controller.front;

import com.bai.edu.client.OrderClient;
import com.bai.edu.entity.Course;
import com.bai.edu.entity.chapter.ChapterVo;
import com.bai.edu.entity.frontvo.CourseFrontVo;
import com.bai.edu.entity.vo.CourseInfoVo;
import com.bai.edu.entity.vo.CourseWebVo;
import com.bai.edu.service.ChapterService;
import com.bai.edu.service.CourseService;
import com.bai.utils.JwtUtils;
import com.bai.utils.R;
import com.bai.utils.orderVo.CourseOrderVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/coursefront")
@CrossOrigin
public class CourseFrontController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private ChapterService chapterService;
    @Autowired
    private OrderClient orderClient;

    //条件查询带分页查询
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(@PathVariable long page,
                                @PathVariable long limit,
                                @RequestBody(required = false) CourseFrontVo courseFrontVo) {
        Page<Course> coursePage = new Page<>(page, limit);
        Map<String, Object> map = courseService.getCourseFrontList(coursePage, courseFrontVo);
        return R.ok().data("map",map);
    }

    //课程详情
    @GetMapping("getFrontCourseInfo/{courseId}")
    public  R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){
        //根据课程id查询课程所有信息
        CourseWebVo courseWebVo=courseService.getBaseCourseInfo(courseId);
        //根据课程id查询章节和小节
        List<ChapterVo> chapterVoList = chapterService.getChapterVideoByCourseId(courseId);

        String memberId= JwtUtils.getMemberIdByJwtToken(request);
        boolean buyCourse = orderClient.isBuyCourse(courseId, memberId);
        return R.ok().data("courseWebVo",courseWebVo).data("chapterVoList",chapterVoList).data("isBuy",buyCourse);
    }

    //根据课程id查询课程信息
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseOrderVo getCourseInfoOrder(@PathVariable String id){
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(id);
        CourseOrderVo courseOrderVo = new CourseOrderVo();
        BeanUtils.copyProperties(courseWebVo,courseOrderVo);
        return courseOrderVo;
    }

}
